package oa.struts.action;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.annotation.Resource;

import org.springframework.beans.BeanUtils;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionContext;

import oa.domain.page.PageBean;
import oa.domain.system.Department;
import oa.domain.system.User;
import oa.service.DepartmentService;
import oa.struts.action.base.BaseAction;
import oa.util.HQLHelper;

@Controller("departmentAction")
public class DepartmentAction extends BaseAction<Department>{

	@Resource(name="departmentService")
	private DepartmentService departmentService;
	public String showAllDepartment()
	{
//         Collection<Department> departmentList = this.departmentService.getAllDeaprtment();	
//         ActionContext.getContext().put("departmentList", departmentList);
		HQLHelper hh = new HQLHelper(Department.class);
		
		 PageBean pageBean = this.departmentService.getPageBean(hh,currentPage);
		 ActionContext.getContext().getValueStack().push(pageBean);
         return listAction;
	}
	
	
	public String addUI()
	{
		return addUI;
	}
	
	public String add()
	{
		Department department = new Department();
		BeanUtils.copyProperties(this.getModel(), department);
		this.departmentService.saveDepartment(department);
		return action2action;
	}
	
	public String updateUI()
	{
		Department department = this.departmentService.getDepartmentById(this.getModel().getDid());
		ActionContext.getContext().getValueStack().push(department);
		return updateUI;
	}
	
	
	
	public String update()
	{
		Department department = this.departmentService.getDepartmentById(this.getModel().getDid());
		BeanUtils.copyProperties(this.getModel(), department);
		this.departmentService.upateDepartment(department);
		return action2action;
	}
	
	
	public String delete()
	{
		Department department = this.departmentService.getDepartmentById(this.getModel().getDid());
		Set<User> users = department.getUsers();
		
		for(User user: users)
		{
			user.setDepartment(null);
		}
		this.departmentService.deleteDepartment(this.getModel().getDid());
		return action2action; 
	}
	
	
	public String showList()
	{
		Department department = new Department();
		department.setName("xxxxx");
		List<Department> dlist = new ArrayList<Department>();
		dlist.add(department);
		ActionContext.getContext().put("dlist", dlist);
		return "listAction";
	}
	
	public String showMap()
	{
		Department department = new Department();
		department.setName("xxxxx");
	    Map<String,Department> map = new HashMap<String, Department>();
	    map.put("department", department);
		ActionContext.getContext().put("dmap", map);
		return "listAction";
	}
	
	public String showListMap()
	{
        List<Map<String,Department>> dlist = new ArrayList<Map<String,Department>>();
		Department department = new Department();
		department.setName("xxxxx");
	    Map<String,Department> map = new HashMap<String, Department>();
	    map.put("department", department);
	    dlist.add(map);
		ActionContext.getContext().put("listmap", dlist);
		return "listAction";
	}
	
	public String showMapList()
	{
		Map<String, List<Department>> map = new HashMap<String, List<Department>>();
		List<Department> list = new ArrayList<Department>();
		Department department = new Department();
		department.setName("xxxxx");
		list.add(department);
		map.put("map", list);
		ActionContext.getContext().put("mapList", map);
		return "listAction";
		
	}
	
	
	public String showListMapList()
	{
		List<Map<String,List<Department>>> list = new ArrayList<Map<String,List<Department>>>();
		Department department = new Department();
		department.setName("xxxxx");
		List<Department> list1 = new ArrayList<Department>();
		list1.add(department);
		
		Map<String,List<Department>> map = new HashMap<String, List<Department>>();
		map.put("map", list1);
		list.add(map);
		
		ActionContext.getContext().put("listMapList", list);
		return "listAction";
	}
	
	
	
	
	
}
